package com.zmh.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.zmh.common.DateUtil;
import com.zmh.common.OperationUtil;
import com.zmh.model.book.BookInfo;
import com.zmh.model.book.ReadSet;
import com.zmh.model.book.ReadSet;

/** 
 * 章节操作
 * @author zhoumh
 * @version 创建时间：2017年7月28日 下午4:47:07 
 * @Description 
*/
@Repository
public class ReadSetDao {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	/**
	 * 查询全部书籍信息
	 * @author zhoumh
	 * @version 创建时间：2017年8月15日 上午11:35:15
	 */
	public List<ReadSet> queryReadSetByAll(){
		String sqlstr = "SELECT id, uuid, user, theme, font, size, state, createtime, updatetime FROM readset";
		List<ReadSet> readsets = new ArrayList<>();
		readsets = jdbcTemplate.query(sqlstr,
				new RowMapper<ReadSet>(){
				@Override
				public ReadSet mapRow(ResultSet rs, int rowNum) throws SQLException {
					return objSet(rs, rowNum);
				}
				});
		System.out.println(readsets.size());
		return readsets;
	}
	/**
	 * 根据UUID查询书籍信息
	 * @author zhoumh
	 * @version 创建时间：2017年8月17日 下午5:19:45
	 */
	public ReadSet queryReadSetByUuid(String uuid){
		String sqlstr = "SELECT id, uuid, user, theme, font, size, state, createtime, updatetime FROM readset WHERE uuid=?";
		ReadSet readset = new ReadSet();
		Object[] param = new Object[]{uuid};
		readset = jdbcTemplate.queryForObject(sqlstr, param, new RowMapper<ReadSet>(){
				@Override
				public ReadSet mapRow(ResultSet rs, int rowNum) throws SQLException {
					return objSet(rs, rowNum);
				}
				});
		return readset;
	}
	
	//增加
	public int insertReadSet(ReadSet readset){
		String sqlstr = "insert into readset (uuid, user, theme, font, size) values(?,?,?,?,?)";
		Object[] param = new Object[]{OperationUtil.getUUID(),readset.getUser(),readset.getTheme(),readset.getFont(),readset.getSize()};
		int result = jdbcTemplate.update(sqlstr, param);
		return result;
		
	}
	//修改
	public int updateReadSet(ReadSet readset){
		String sqlstr = "update readset set user=?, theme=?, font=?, size=?, state=?, updatetime=? where uuid=?";
		Object[] param = new Object[]{readset.getUser(),readset.getTheme(),readset.getFont(),readset.getSize(),readset.getState(),DateUtil.getDateTime(),readset.getUuid()};
		int result = jdbcTemplate.update(sqlstr, param);
		return result;
	}
	//删除
	public int deleteReadSetByUuid(String uuid){
		String sqlstr = "delete from readset where uuid = ?";
		Object[] param = new Object[]{uuid};
		int result = jdbcTemplate.update(sqlstr, param);
		return result;
	}
	/*
	 * 数据库查询结果映射
	 * @author zhoumh
	 * @version 创建时间：2017年8月15日 上午11:36:14
	 */
	public ReadSet objSet(ResultSet rs, int rowNum) throws SQLException {
		ReadSet readset = new ReadSet();
		readset.setId(rs.getInt("id"));
		readset.setUuid(rs.getString("uuid"));
		readset.setUser(rs.getString("user"));
		readset.setTheme(rs.getString("theme"));
		readset.setFont(rs.getString("font"));
		readset.setSize(rs.getString("size"));
		readset.setState(rs.getString("state"));
		readset.setCreateTime(rs.getString("createtime"));
		readset.setUpdateTime(rs.getString("updatetime"));
		return readset;
	}
	
}
